package com.pubinfo.passbook.data.controller.forward;

import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.entity.TFileEnclosureFtp;
import com.pubinfo.passbook.common.entity.TFileInfo;
import com.pubinfo.passbook.common.entity.TFileOpenEnclosureFtp;
import com.pubinfo.passbook.common.mapper.TWiteoffInstanceMapper;
import com.pubinfo.passbook.common.model.dto.file.ArtificialFileParam;
import com.pubinfo.passbook.common.model.dto.file.FileListParam;
import com.pubinfo.passbook.common.model.dto.file.UploadFileParam;
import com.pubinfo.passbook.common.model.dto.open.WiteoffAttchment;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.data.service.DownLoadPDFService;
import com.pubinfo.passbook.data.service.FtpService;
import com.pubinfo.passbook.data.service.TFileInfoService;
import com.pubinfo.passbook.data.service.VoucherFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping(value = "file")
@Api(tags = "文件信息")
@Slf4j
public class TFileController {
  @Autowired
  TFileInfoService tFileInfoService;
  @Autowired
  DownLoadPDFService downLoadPDFByEInvoiceId;
  @Autowired
  TWiteoffInstanceMapper instanceMapper;
  @Autowired
  VoucherFileService voucherFileService;
  @Autowired
  FtpService ftpService;

  @PostMapping("/uploadFile")
  @ApiOperation(value = "通过id上传发票或票据的文件,type为1为票据2为发票")
  public JsonResult<Boolean> uploadInvoiceById(UploadFileParam uploadFileParam) throws IOException {
    return JsonResult.success(tFileInfoService.uploadFileByType(uploadFileParam.getFile(), uploadFileParam.getType(), uploadFileParam.getId()));
  }
  
  @PostMapping("/selectFile")
  @ApiOperation(value = "查询文件")
  public JsonResult<MyPage<TFileInfo>> selectAllFileInfo(@RequestBody FileListParam fileListParam) throws IOException {
    return JsonResult.success(new MyPage(tFileInfoService.selectFileInfoByFileListParam(fileListParam)));
  }
  
  @GetMapping("list/{artificialId}")
  @ApiOperation(value = "根据人工上传id查询所有的文件")
  public JsonResult<List<TFileInfo>> listByInvoiceId(@PathVariable("artificialId") String artificialId) {
    return JsonResult.success(tFileInfoService.selectByArtificialId(artificialId));
  }
  
  @DeleteMapping("/removeFile")
  @ApiOperation(value = "软删除文件")
  public JsonResult<Boolean> removeFile(@RequestBody List<String> id) {
    if(CollectionUtils.isEmpty(id))return JsonResult.error("数组为空");
    return JsonResult.success(tFileInfoService.softDelete(id));
  }
  
  @PostMapping("/downLoadPDFByEInvoiceId")
  @ApiOperation(value = "下载指定发票号码的电子票据")
  public JsonResult<Boolean> downLoadPDFByEInvoiceId(String eInvoiceId) throws IOException, UnirestException {
    downLoadPDFByEInvoiceId.downLoadPDFByEInvoiceId(eInvoiceId);
    return JsonResult.success(true);
  }
  
  @PostMapping("/saveTFileEnclosure")
  @ApiOperation(value = "上传票据相关附件")
  public JsonResult<TFileEnclosureFtp> saveTFileEnclosure(MultipartFile file) throws IOException {
    return JsonResult.success(tFileInfoService.saveTFileEnclosure(file));
  }

  @GetMapping("/acctDownload/{id}")
  @ApiOperation(value = "用户根据文件id下载文件")
  public void acctDownload(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) throws UnsupportedEncodingException {
    tFileInfoService.acctDownload(request, response, id);
  }

  @PostMapping("/acctDownloadZip")
  @ApiOperation(value = "用户根据文件id下载多个文件的压缩包")
  public void acctDownloadZip(HttpServletRequest request, HttpServletResponse response, @RequestBody List<String> id) throws IOException {
    tFileInfoService.acctDownloadZip(request, response, id);
  }
  
  @PostMapping("/uploadArtificialFile")
  @ApiOperation(value = "上传人工报账相关文件")
  public JsonResult<Boolean> uploadArtificialFile(ArtificialFileParam artificialFileParam) throws IOException {
    return JsonResult.success(tFileInfoService.saveArtificialFile(artificialFileParam));
  }
  
  @PostMapping("uploadOpenFile")
  @ApiOperation(value = "上传开放报账接口文件")
  public JsonResult<List<WiteoffAttchment>> uploadOpenFile(@RequestBody List<WiteoffAttchment> attchments) {
    return JsonResult.success(tFileInfoService.uploadOpenFile(attchments));
  }

//  @PostMapping("odf2Json")
//  @ApiOperation(value = "解析odf文件")
//  public JsonResult<JSONObject> odf2Json(String fileId) throws Exception {
//    return JsonResult.success(voucherFileService.odf2Json(fileId));
//  }


  @PostMapping("downDetailPdf")
  @ApiOperation(value = "下载详情pdf")
  public JsonResult<Boolean> downDetailPdf(@RequestBody List<String> invoiceIds) throws UnirestException, IOException {
    return JsonResult.success(downLoadPDFByEInvoiceId.downDetailPdf(invoiceIds));
  }
  
  @PostMapping("file2Ftp")
  @ApiOperation(value = "测试文件上传ftp")
  public JsonResult<TFileOpenEnclosureFtp> file2Ftp(String fileId) {
    TFileInfo tFileInfo = tFileInfoService.getById(fileId);
    return JsonResult.success(ftpService.uploadOpenEnclosureFile(tFileInfo));
  }

  @PostMapping("ofd2JSON")
  @ApiOperation(value = "测试ofd解析")
  public JsonResult<JSONObject> ofd2JSON(String fileId) throws Exception {
    return JsonResult.success(voucherFileService.invoiceOdf2Json(fileId));
  }

}

